<?php
namespace app\api\controller\amount;

use app\common\controller\Api;
use app\api\controller\amount\Curl;
use think\Request;
use think\Cache;
use think\Db;
use think\Log;

class Amount extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight='*';
    private $app_id = 1779971008866359;
    private $secret = '8864c1f31ce5e3d713b81f43fd4832d4d32785de';
    //获取access token GET
    public function get_token(){
        $auth_code = $this->request->param('auth_code');
        // 构造POST请求的数据
        $data = [
            'app_id' => $this->app_id,
            'secret' => $this->secret,
            'grant_type'=>"auth_code",
            'auth_code' => $auth_code,
        ];
        // 发起POST请求
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/oauth2/access_token/',
            'method'=>'POST',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer"
            ],
        ],$data);
        // 从 Redis 中检索数据

        $body =  json_decode($res['body']);
        if($body->code == 0){
            $redis = new \Redis();
            $redis->connect('127.0.0.1', 6379); // 连接到 Redis 服务器，指定主机和端口
            $value = $redis->get('juliang_token'); // 从 Redis 获取值
            $redis->set('juliang_token', $body->data->access_token); // 存储键值对
            $redis->set('ju_refresh_token', $body->data->refresh_token); // 存储键值对
            $redis->close();
        }
        return $this->success('返回成功', json_decode($res['body']));
    }
    //刷新access_token POST
    public function refresh_token(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379); // 连接到 Redis 服务器，指定主机和端口
        $refresh_token = $redis->get('ju_refresh_token');
        $access_token = $redis->get('juliang_token');// 从 Redis 获取值
        var_dump($refresh_token);
        $data = [
            'app_id' => $this->app_id,
            'secret' => $this->secret,
            "grant_type"=>"refresh_token",
            'refresh_token' => $refresh_token,
        ];
        // 发起POST请求
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/oauth2/refresh_token/',
            'method'=>'POST',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer"
            ],
        ],$data);
        $body = json_decode($res['body']);
        try{
            $redis->set('juliang_token', $body->data->access_token); // 存储键值对
            $redis->set('ju_refresh_token', $body->data->refresh_token); // 存储键值对
            $access_token = $redis->get('juliang_token');
            Log::info('刷新巨量token成功');
            Log::info($access_token);
            $redis->close();
        }catch (Exception $exception){
            Log::error($exception);
        }


        return $this->success('返回成功', json_decode($res['body']));
    }
    //获取已授权账户
    public function advertisers(){
        $access_token = $this->request->param('access_token');
        $data = [
            'access_token' => $access_token,
        ];
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/oauth2/advertiser/get/',
            'method'=>'GET',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer"
            ],
        ],$data);
        return $this->success('返回成功', json_decode($res['body']));
    }
    //获取广告组
    public function campaign(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379); // 连接到 Redis 服务器，指定主机和端口
        $access_token = $redis->get('juliang_token'); // 从 Redis 获取值
        $redis->close();
        $data = [
            'advertiser_id' => '58493082977'
        ];
        // 发起POST请求
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/2/advertiser/info/',
            'method'=>'GET',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer",
                "Access-Token:".$access_token
            ],
        ],$data);
        $body = json_decode($res['body']);
        return $this->success('返回成功', $body);
    }
    //获取广告主数据 GET
    public function advertisers_data(){
        $access_token = $this->request->param('access_token');
        $start_date = $this->request->param('start_date');
        $end_date = $this->request->param('end_date');
        $advertiser_ids = $this->request->param('advertiser_ids');
        $advertiserData = [];
        $advertiser_ids = json_decode($advertiser_ids);
        foreach ($advertiser_ids as $advertiser_id){
            $data = [
                'advertiser_id' => $advertiser_id,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'fields'=>[
                    "cost","click","convert", "avg_show_cost", "convert_rate", "convert_cost", "avg_click_cost", "show", "ctr",
                    "deep_convert",'deep_convert_cost','deep_convert_rate',
                ]
            ];
            // 发起POST请求
            $curl = new Curl();
            $res = $curl->curlRequest([
                'url'=>'https://ad.oceanengine.com/open_api/2/report/advertiser/get/',
                'method'=>'GET',
                'headers'=>[
                    "Content-Type:application/json;charset=UTF-8",
                    "User-Agent:API Explorer",
                    "Access-Token:".$access_token
                ],
            ],$data);
            $resData = json_decode($res['body']);
            if($resData->code == 0) $advertiserData[] = $resData->data;
        }

        return $this->success('返回成功', $advertiserData);
    }
    //获取纵横组织下账户列表
    public function advertisers_select(){

        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379); // 连接到 Redis 服务器，指定主机和端口
        $access_token = $redis->get('juliang_token'); // 从 Redis 获取值
        $redis->close();
        $data = [
            'advertiser_id' => '1668381887237127'
        ];
        $curl = new Curl();
        $res = $curl->curlRequest([
            'url'=>'https://ad.oceanengine.com/open_api/2/majordomo/advertiser/select/',
            'method'=>'GET',
            'headers'=>[
                "Content-Type:application/json;charset=UTF-8",
                "User-Agent:API Explorer",
                "Access-Token:".$access_token
            ],
        ],$data);
        $body = json_decode($res['body']);
        return $this->success('返回成功', $body);
    }
    //获取我的总收益
    public function revenue(Request $request){
        $id = $request->param('id');
        $user = Db::name('user')->where(['id'=>$id])->find();
        return $this->success('返回成功', $user);
    }

}